package com.easychat.utils;

import com.easychat.entity.constants.Constants;
import com.easychat.enums.UserContactTypeEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

/**
 * @author 10 2024/10/4
 */
public class StringTools {
     public static boolean isEmpty(String str){
          if(null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)){
               return true;
          }else if("".equals(str.trim())) {
               return true;
          }
          return false;
     }
     public static String getUserId(){
          return UserContactTypeEnum.USER.getPrefix() + getRandomNumber(Constants.RANDOM_COUNT);
     }
     public static String getGroupId(){
          return UserContactTypeEnum.GROUP.getPrefix() + getRandomNumber(Constants.RANDOM_COUNT);
     }

     public static String getRandomNumber(Integer count){
          return RandomStringUtils.random(count,false,true);
     }
     public static String getRandomString(Integer count){
          return RandomStringUtils.random(count,true,true);
     }
     public static String encodeMd5(String originPass){
          return StringTools.isEmpty(originPass) ? null : DigestUtils.md5Hex(originPass);
     }

     public static String cleanHtmlTag(String content) {
          if(isEmpty(content)){
               return content;
          }
          //对消息转译
          content = content
                  .replace("<","&lt;")
                  .replace("\r\n","<br>")
                  .replace("\n","<br>");

          return content;
     }
     //获取会话id ，将两个用户先排序，然后拼到一起，再使用md5加密
     public static final String getChatSessionId4User(String[] userIds){
          Arrays.sort(userIds);
          return encodeMd5(StringUtils.join(userIds,""));
     }


     public static String getChatSessionId4Group(String contactId) {
          return encodeMd5(contactId);

     }

     public static String getFileSuffix(String fileName){
          if(fileName.isEmpty()){
               return null;
          }
          return fileName.substring(fileName.lastIndexOf("."));
     }

     public static boolean isNumber(String str){
          String checkNumber = "^[0-9]+$";
          if(null == str){
               return false;
          }
          if(!str.matches(checkNumber)){
               return false;
          }
          return true;
     }


}
